// 购物车点击事件
$(function () {
    // 点击显示隐藏
    $('.cxdli3').on('click', function () {
        if ($('.cart_commodity').width() == 0) {
            $('.cart_commodity').animate({
                'width': 280,
            }, 500);
            /*要显示数据*/
            goods();
            /*绑定事件删除*/
            delSp();
            /*商品数量*/
            spCount();
        } else {
            $('.cart_commodity').animate({
                'width': 0,
            }, 500)
        }
    });


    // 悬浮的购物车
    $(".gouwuche").one("mouseenter", function () {
        /*要显示数据*/
        goods();
    })
    $(".gouwuche").hover(function () {
        $(".h-r-box").show();

        /*绑定事件删除*/
        delSp();
        /*商品数量*/
        spCount();
    }, function () {
        $(".h-r-box").hide();
    })
    $(".cxdsp").css("background-color", "red")
    $('.cxdli3').hover(function () {
        $(".cxdsp").css("background-color", "black")
    }, function () {
        $(".cxdsp").css("background-color", "red")
    })


    // btn-add数量加减
    $('.btn-add').on('click', function () {
        var num = $(this).parent().prev().val();
        num++;
        $(this).parent().prev().val(num);
    });
    $('.btn-reduce').on('click', function () {
        var num = $(this).parent().prev().val();
        num--;
        if (num < 1) {
            num = 1;
        }
        $(this).parent().prev().val(num);
    });
    /*加入购物车*/
    let addSp = $(".btn-append");
    // let num = 0;
    //商品的数量
    let cxdsp = $(".cxdsp");
    let spId = location.hash.substring(1);
    let spName = location.search.split("=")[1];
    let spIdS = "";
    addSp.click(function () {
        if (confirm("要购买商品吗？")) {
            /*存储商品的id goodsId = id*/
            spId = location.hash.substring(1);
            localStorage.setItem("goodsId" + spId, spId);
            let geShu = Number($(".buy-num").val());
            let id = localStorage.getItem(spId);
            /*判断商品的id是否存在*/
            if (id != null) {
                /*存在进行++*/
                localStorage.setItem(spId, geShu + Number(id));
            } else {
                /*商品的id保存*/
                // spIdS+="goodsId"+ spId;
                localStorage.setItem("spIdS", localStorage.getItem("spIdS") + "goodsId" + spId + ",");
                /*不存在初始值*/
                localStorage.setItem(spId, geShu);
            }
            // 更新数据
            spCount();
        }

    });

    /*购物车的数据*/
    function goods() {
        let item = localStorage.getItem("spIdS");
        let obj = [];
        Object.keys(data).forEach(function (key) {
            /*找到符合条件的对象 并返回*/
            data[key].filter(function (v) {
                if (item != null && item.includes("goodsId" + v.goodsid + ",")) {
                    obj.push(v);
                }
            })
        });
        // let obj = data[spName].filter(function (v, i) {
        /*找到符合条件的对象 并返回*/
        // if (item != null && item.includes("goodsId" + v.goodsid + ",")) {
        //     return v;
        // }
        // })
        localStorage.setItem("dataArr", JSON.stringify(obj))
        let k_con = $(".K_CC-content");
        /*追加的商品*/
        let k_li = "";
        obj.map(function (v, i) {
            k_li += ` <li class="K-CCC-li">
                    <div class="K-CCCL-img">
                        <img src="${v.goodsurls.nowurl}" alt="">
                    </div>
                    <div class="K-CCCL-text" data-id=${v.goodsid}>
                        <p class="K-CCCLT-p1"><span class="K-CCCLTP-span1">${v.goodstitle}</span></p>
                        <p class="K-CCCLTP-span2 fc-red fz12">${v.goodszprice}</p>
                        <p class="K-CCCLT-p2"> <span style="display: inline-block"><button class="btnSub">-</button><span>${localStorage.getItem(v.goodsid)}</span><button class="btnAdd">+</button></span><a href="javascript:void(0)" class="fz12 ml-40 delSp" data-goodsId="${v.goodsid}">删除</a></p>
                    </div>
                </li>`
        })
        /*追加商品*/
        k_con.empty();
        k_con.append(k_li);

        // 侧边购物车加减
        $('.btnAdd').on('click', function () {
            let num = parseInt($(this).prev().html());
            num++;
            $(this).prev().html(num);
            spId = $(this).parents(".K-CCCLT-p2").children("a").attr('data-goodsid')
            localStorage.setItem("goodsId" + spId, spId);
            let id = localStorage.getItem(spId);
            /*判断商品的id是否存在*/
            if (id != null) {
                /*存在进行++*/

                localStorage.setItem(spId, 1 + Number(id));
            } else {
                /*商品的id保存*/
                // spIdS+="goodsId"+ spId;
                localStorage.setItem("spIdS", localStorage.getItem("spIdS") + "goodsId" + spId + ",");
                /*不存在初始值*/
                localStorage.setItem(spId, geShu);
            }
            // 更新数据
            spCount();
        });
        $('.btnSub').on('click', function () {
            let num = parseInt($(this).next().html());
            num--;
            if (num < 1) {
                num = 1;
            }
            spId = $(this).parents(".K-CCCLT-p2").children("a").attr('data-goodsid')
            localStorage.setItem("goodsId" + spId, spId);

            let id = localStorage.getItem(spId);
            /*判断商品的id是否存在*/
            if (id != null) {
                /*存在进行++*/
                localStorage.setItem(spId, num);
            } else {
                /*商品的id保存*/
                // spIdS+="goodsId"+ spId;
                localStorage.setItem("spIdS", localStorage.getItem("spIdS") + "goodsId" + spId + ",");
                /*不存在初始值*/
                localStorage.setItem(spId, geShu);
            }
            // 更新数据
            spCount();

            $(this).next().html(num);
        });

    }

    /*删除购物车*/
    function delSp() {
        $(".delSp").click(function () {
            console.log($(this).index(), "qq");
            /*商品的id*/
            let idS = $(this).attr("data-goodsId");
            let item = localStorage.getItem("spIdS");
            // /*获取新的*/
            item = item.replaceAll("goodsId" + idS + ",", "");
            let obj = [];
            Object.keys(data).forEach(function (key) {
                /*找到符合条件的对象 并返回*/
                data[key].filter(function (v) {
                    if (item != null && item.includes("goodsId" + v.goodsid + ",")) {
                        obj.push(v);
                    }
                })
            });
            // let obj = data[spName].filter(function (v, i) {
            //     //     /*找到符合条件的对象 并返回*/
            //     if (item != null && item.includes("goodsId" + v.goodsid + ",")) {
            //         return v;
            //     }
            // })
            // /*更新数据*/
            $(this).parents(".K-CCC-li").remove();//删除商品
            localStorage.removeItem(idS);
            localStorage.removeItem("goodsId" + idS);
            localStorage.setItem("spIdS", item);
            localStorage.setItem("dataArr", JSON.stringify(obj));
            /*刷新界面,调用*/
            // location.reload();
            spCount()
        })
    }

    /*商品的小圆点*/
    spCount();

    function spCount() {
        /*头部的商品个数*/
        let sum = $("#sum");
        let sum2 = $(".zgs");
        /*侧边栏的商品个数*/
        let cxdsp = $(".cxdsp");
        /*每个商品的id不一致*/
        let spidS = localStorage.getItem("spIdS");
        if (spidS != null) {
            spidS = spidS.substring(4)
        }
        /**/
        /*调用截取的id*/
        let virtualId = draw(spidS);
        let spNumber = 0;
        virtualId.forEach(function (v, i) {
            /*获取真实商品的id*/
            let realId = localStorage.getItem(v);
            /*计算商品的个数*/
            spNumber += Number(localStorage.getItem(realId));
        })
        /*商品的数量更新*/
        sum.html(spNumber);
        sum2.html(spNumber);
        cxdsp.css("background-color", "red");
        cxdsp.html(spNumber);
    }

    /*提取商品的id*/
    function draw(str) {
        /*虚拟的id*/
        let arrId = [];
        let index = 0;
        let initial = -1;
        while ((index = str.indexOf(",", initial + 1)) != -1) {
            // console.log(initial,index)
            arrId.push(str.substring(initial + 1, index));
            initial = index;
        }
        // console.log(arrId)
        return arrId;
    }

    // 关闭按钮
    $(".K_CCT-a").on("click", function () {
        $('.cart_commodity').animate({
            'width': 0,
        }, 500)
    });
});

